package com.hww.gateway.config;

import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;

/**
 * @Author: Wenwu Huang
 * @Date: 2018/10/9 20:41
 */
@Configuration
public class ExampleConfiguration {

//    @Bean
//    @Order(-1)
//    public GlobalFilter a() {
//        return (exchange, chain) -> {
//            String jwtToken = exchange.getRequest().getHeaders().getFirst("Authorization");
//            //校验jwtToken的合法性
//            if (jwtToken != null) {
//                // 合法
//                // 将用户id作为参数传递下去
//                return chain.filter(exchange);
//            }
//
//            //不合法(响应未登录的异常)
//            ServerHttpResponse response = exchange.getResponse();
//            String warningStr = "no session";
//            DataBuffer bodyDataBuffer = response.bufferFactory().wrap(warningStr.getBytes());
//            return response.writeWith(Mono.just(bodyDataBuffer));
//        };
//    }
}
